package com.beidouin.GnssMain.sample;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.beidouin.GnssMain.EventSerialReceive;
import com.beidouin.GnssMain.FileManager;
import com.beidouin.GnssMain.R;
import com.beidouin.GnssMain.utils.DialogUtil;
import com.beidouin.GnssMain.utils.FileUtils;
import com.friendlyarm.AndroidSDK.FileCtlEnum;
import com.friendlyarm.AndroidSDK.HardwareControler;
import com.r0adkll.postoffice.styles.ListStyle;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import static com.beidouin.GnssMain.FileManager.scenarioNameList;

public class ConsoleActivity extends SerialPortActivity implements View.OnClickListener {

    private String devName = "dev/ttyAMA2";
    private String devName2 = "dev/ttyAMA3";
    private String fileContent;
    private String instruct = "";
    private int dev = -1;
    private int devFD = -1;
    private int devFD2 = -1;
    private String path;
    private String s;
    private int speed = 115200;
    private int dataBits = 8;
    private int stopBits = 1;
    TextView aboutusTextView;
    RelativeLayout mrelative, mrelati;

    TextView timerTextView, recordTextView, pauseTextView, playTextView,
            totalTimeTextView, deleteTextView, aboutusLayout, butofflice;
    RelativeLayout recordLayout, pauseLayout, playLayout, deleteLayout;

    private ArrayAdapter<String> arrayAdapter;
    private int currentAction = 0; //0 表示回放，1表示删除

    @Override
    public void onDestroy() {
        if (dev >= 0) {
            HardwareControler.close(dev);
            HardwareControler.close(devFD);
            HardwareControler.close(devFD2);
            dev = -1;
        }
        super.onDestroy();
    }
   public char[] strrxmsavpos6;
   public byte[] arrayDatarxmsave6;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_console);
        path = Environment.getExternalStorageDirectory().getPath();
        //串口通信名+地址103+104
        dev = HardwareControler.open("/dev/i2c-0", FileCtlEnum.O_RDWR);
        int rew = HardwareControler.setI2CSlave(dev, 0x29);//设置I2c通信硬件地址
        //串口1
        devFD = HardwareControler.openSerialPort(devName, speed, dataBits, stopBits);
        //串口2
        devFD2 = HardwareControler.openSerialPort(devName2, speed, dataBits, stopBits);

        initBottomView();

        mrelative = (RelativeLayout) findViewById(R.id.relative);
        mrelati = (RelativeLayout) findViewById(R.id.relati);
        consoleImage.setImageResource(R.drawable.action_selected);
        consoleText.setTextColor(getResources().getColor(R.color.app_main));
        butofflice = (TextView) findViewById(R.id.butoffline);

        butofflice.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(ConsoleActivity.this, OfflineDemo.class));
            }
        });
        mrelative.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                s = "$CCSET,GPS11," + "GPS12\r\n";
                SendI2cCmd(s);

                Sleep();

//                String strloc = "$PCAS04,7*1E\r\n";
//                byte[] loc = strloc.getBytes();
//                HardwareControler.write(devFD, loc);
                String strloc = "$PCAS04,3*1A\r\n";
                byte[] loc = strloc.getBytes();
                HardwareControler.write(devFD, loc);

                Sleep();

                char[] serialport = {0xBA, 0xCE, 0x08, 0x00, 0x06, 0x00, 0xFF, 0x22, 0xC0, 0x08, 0x00, 0xC2, 0x01, 0x00, 0x07, 0xE5, 0xC7, 0x08};
                byte[] arrayData = new byte[serialport.length];
                for (int i = 0; i < serialport.length; ++i)
                    arrayData[i] = (byte) serialport[i];
                HardwareControler.write(devFD, arrayData);

                Sleep();

                char[] strgga = {0xBA, 0xCE, 0x04, 0x00, 0x06, 0x01, 0x4E, 0x00, 0x01, 0x00, 0x52, 0x00, 0x07, 0x01};
                byte[] arrayDatagga = new byte[strgga.length];
                for (int i = 0; i < strgga.length; ++i)
                    arrayDatagga[i] = (byte) strgga[i];
                HardwareControler.write(devFD, arrayDatagga);

                Sleep();

                char[] strgsa = {0xBA, 0xCE, 0x04, 0x00, 0x06, 0x01, 0x4E, 0x02, 0x01, 0x00, 0x52, 0x02, 0x07, 0x01};
                byte[] arrayDatagsa = new byte[strgsa.length];
                for (int i = 0; i < strgsa.length; ++i)
                    arrayDatagsa[i] = (byte) strgsa[i];
                HardwareControler.write(devFD, arrayDatagsa);

                Sleep();

                char[] strgsv = {0xBA, 0xCE, 0x04, 0x00, 0x06, 0x01, 0x4E, 0x03, 0x01, 0x00, 0x52, 0x03, 0x07, 0x01};
                byte[] arrayDatagsv = new byte[strgsv.length];
                for (int i = 0; i < strgsv.length; ++i)
                    arrayDatagsv[i] = (byte) strgsv[i];
                HardwareControler.write(devFD, arrayDatagsv);

                char[] strgls = {0xBA, 0xCE, 0x04, 0x00, 0x06, 0x01, 0x4E, 0x04, 0x01, 0x00, 0x52, 0x04, 0x07, 0x01};
                byte[] arrayDatagls = new byte[strgls.length];
                for (int i = 0; i < strgls.length; ++i)
                    arrayDatagls[i] = (byte) strgls[i];
                HardwareControler.write(devFD, arrayDatagls);

                Sleep();

                String strsave = "$PCAS00*01\r\n";
                byte[] bytes2save = strsave.getBytes();
                HardwareControler.write(devFD, bytes2save);

                Sleep();

                char[] strserialport2 = {0xBA, 0xCE, 0x08, 0x00, 0x06, 0x00, 0xFF, 0x11, 0xC0, 0x08, 0x00, 0xC2, 0x01, 0x00, 0x07, 0xD4, 0xC7, 0x08};
                byte[] arrayDatapor2 = new byte[strserialport2.length];
                for (int i = 0; i < strserialport2.length; ++i)
                    arrayDatapor2[i] = (byte) strserialport2[i];
                HardwareControler.write(devFD2, arrayDatapor2);

                Sleep();

                char[] strdop = {0xBA, 0xCE, 0x04, 0x00, 0x06, 0x01, 0x01, 0x01, 0x01, 0x00, 0x05, 0x01, 0x07, 0x01};
                byte[] arrayDatadop = new byte[strdop.length];
                for (int i = 0; i < strdop.length; ++i)
                    arrayDatadop[i] = (byte) strdop[i];
                HardwareControler.write(devFD2, arrayDatadop);

                Sleep();

                char[] strsol = {0xBA, 0xCE, 0x04, 0x00, 0x06, 0x01, 0x01, 0x02, 0x01, 0x00, 0x05, 0x02, 0x07, 0x01};
                byte[] arrayDatasol = new byte[strsol.length];
                for (int i = 0; i < strsol.length; ++i)
                    arrayDatasol[i] = (byte) strsol[i];
                HardwareControler.write(devFD2, arrayDatasol);

                Sleep();

                char[] strpv = {0xBA, 0xCE, 0x04, 0x00, 0x06, 0x01, 0x01, 0x03, 0x01, 0x00, 0x05, 0x03, 0x07, 0x01};
                byte[] arrayDatapv = new byte[strpv.length];
                for (int i = 0; i < strpv.length; ++i)
                    arrayDatapv[i] = (byte) strpv[i];
                HardwareControler.write(devFD2, arrayDatapv);

                Sleep();

                char[] strgpsinfo = {0xBA, 0xCE, 0x04, 0x00, 0x06, 0x01, 0x01, 0x20, 0x01, 0x00, 0x05, 0x20, 0x07, 0x01};
                byte[] arrayDatainfo = new byte[strgpsinfo.length];
                for (int i = 0; i < strgpsinfo.length; ++i)
                    arrayDatainfo[i] = (byte) strgpsinfo[i];
                HardwareControler.write(devFD2, arrayDatainfo);

                Sleep();

                char[] strbdsinfo = {0xBA, 0xCE, 0x04, 0x00, 0x06, 0x01, 0x01, 0x21, 0x01, 0x00, 0x05, 0x21, 0x07, 0x01};
                byte[] arrayDatabdsinfo = new byte[strbdsinfo.length];
                for (int i = 0; i < strbdsinfo.length; ++i)
                    arrayDatabdsinfo[i] = (byte) strbdsinfo[i];
                HardwareControler.write(devFD2, arrayDatabdsinfo);

                Sleep();

                char[] strglninfo = {0xBA, 0xCE, 0x04, 0x00, 0x06, 0x01, 0x01, 0x22, 0x01, 0x00, 0x05, 0x22, 0x07, 0x01};
                byte[] arrayDataglninfo = new byte[strglninfo.length];
                for (int i = 0; i < strglninfo.length; ++i)
                    arrayDataglninfo[i] = (byte) strglninfo[i];
                HardwareControler.write(devFD2, arrayDataglninfo);

                Sleep();

                char[] strrxmmeasx = {0xBA, 0xCE, 0x04, 0x00, 0x06, 0x01, 0x03, 0x10, 0x01, 0x00, 0x07, 0x10, 0x07, 0x01};
                byte[] arrayDatarxmeas = new byte[strrxmmeasx.length];
                for (int i = 0; i < strrxmmeasx.length; ++i)
                    arrayDatarxmeas[i] = (byte) strrxmmeasx[i];
                HardwareControler.write(devFD2, arrayDatarxmeas);

                Sleep();

                char[] strrxmsavpos = {0xBA, 0xCE, 0x04, 0x00, 0x06, 0x01, 0x03, 0x11, 0x01, 0x00, 0x07, 0x11, 0x07, 0x01};
                byte[] arrayDatarxmsave = new byte[strrxmsavpos.length];
                for (int i = 0; i < strrxmsavpos.length; ++i)
                    arrayDatarxmsave[i] = (byte) strrxmsavpos[i];
                HardwareControler.write(devFD2, arrayDatarxmsave);
                //06
//                 strrxmsavpos6 = new char[]{0xBA, 0xCE, 0x04, 0x00, 0x06, 0x01, 0x08, 0x00, 0x00, 0x00, 0x0C, 0x00, 0x06, 0x01};
//                arrayDatarxmsave6 = new byte[strrxmsavpos6.length];
//                for (int i = 0; i < strrxmsavpos6.length; ++i)
//                    arrayDatarxmsave6[i] = (byte) strrxmsavpos6[i];
//                HardwareControler.write(devFD2, arrayDatarxmsave6);
//
//                strrxmsavpos6 = new char[]{0xBA, 0xCE, 0x04, 0x00, 0x06, 0x01, 0x08, 0x01, 0x00, 0x00, 0x0C, 0x01, 0x06, 0x01};
//                arrayDatarxmsave6 = new byte[strrxmsavpos6.length];
//                for (int i = 0; i < strrxmsavpos6.length; ++i)
//                    arrayDatarxmsave6[i] = (byte) strrxmsavpos6[i];
//                HardwareControler.write(devFD2, arrayDatarxmsave6);
//
//                strrxmsavpos6 = new char[]{0xBA, 0xCE, 0x04, 0x00, 0x06, 0x01, 0x08, 0x02, 0x00, 0x00, 0x0C, 0x02, 0x06, 0x01};
//                arrayDatarxmsave6 = new byte[strrxmsavpos6.length];
//                for (int i = 0; i < strrxmsavpos6.length; ++i)
//                    arrayDatarxmsave6[i] = (byte) strrxmsavpos6[i];
//                HardwareControler.write(devFD2, arrayDatarxmsave6);
//
//                strrxmsavpos6 = new char[]{0xBA, 0xCE, 0x04, 0x00, 0x06, 0x01, 0x08, 0x05, 0x00, 0x00, 0x0C, 0x05, 0x06, 0x01};
//                arrayDatarxmsave6 = new byte[strrxmsavpos6.length];
//                for (int i = 0; i < strrxmsavpos6.length; ++i)
//                    arrayDatarxmsave6[i] = (byte) strrxmsavpos6[i];
//                HardwareControler.write(devFD2, arrayDatarxmsave6);
//
//                strrxmsavpos6 = new char[]{0xBA, 0xCE, 0x04, 0x00, 0x06, 0x01, 0x08, 0x06, 0x00, 0x00, 0x0C, 0x06, 0x06, 0x01};
//                arrayDatarxmsave6 = new byte[strrxmsavpos6.length];
//                for (int i = 0; i < strrxmsavpos6.length; ++i)
//                    arrayDatarxmsave6[i] = (byte) strrxmsavpos6[i];
//                HardwareControler.write(devFD2, arrayDatarxmsave6);
//
//                strrxmsavpos6 = new char[]{0xBA, 0xCE, 0x04, 0x00, 0x06, 0x01, 0x08, 0x07, 0x00, 0x00, 0x0C, 0x07, 0x06, 0x01};
//                arrayDatarxmsave6 = new byte[strrxmsavpos6.length];
//                for (int i = 0; i < strrxmsavpos6.length; ++i)
//                    arrayDatarxmsave6[i] = (byte) strrxmsavpos6[i];
//                HardwareControler.write(devFD2, arrayDatarxmsave6);

                Sleep();

                String strsave2 = "$PCAS00*01\r\n";
                byte[] bytes2save2 = strsave2.getBytes();
                HardwareControler.write(devFD2, bytes2save2);

                Sleep();

                fileContent = FileUtils.getFileContent(new File(path + "/storage/emulated/0/" + "iN6100_data/NMEA/" + "NMEA.txt"));
                SendI2cCmd(fileContent);
            }
        });
        //配置模块2
        mrelati.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                s = "$CCSET,GPS23," + "GPS24\r\n";
                SendI2cCmd(s);

                Sleep();

                String setnm = "setNMEAOutput, Stream1, COM1\r\n";
                byte[] bytes2savesetnm = setnm.getBytes();
                HardwareControler.write(devFD, bytes2savesetnm);

                Sleep();

                String stream2 = "setNMEAOutput, Stream1, , GGA+GSA+GSV+RMC\r\n";
                byte[] bytes2savesetnm2 = stream2.getBytes();
                HardwareControler.write(devFD, bytes2savesetnm2);

                Sleep();

                String stream1 = "setNMEAOutput, Stream1, , , sec1\r\n";
                byte[] bytes2savesetnm1 = stream1.getBytes();
                HardwareControler.write(devFD, bytes2savesetnm1);

                Sleep();

                String com2 = "setDataInOut, COM2, , RTCMv3\r\n";
                byte[] bytescom2 = com2.getBytes();
                HardwareControler.write(devFD, bytescom2);

                Sleep();

                String com3 = "setRTCMv3Output,COM2,RTCM1001+RTCM1002+RTCM1003+RTCM1004+RTCM1005+RTCM1006+RTCM1007+RTCM1008+RTCM1009+RTCM1010+RTCM1011+RTCM1012+RTCM1013+RTCM1019+RTCM1020+RTCM1029+RTCM1033+RTCM1042+RTCM1044+RTCM1045+RTCM1046+RTCM1071+RTCM1072+RTCM1073+RTCM1074+RTCM1075+RTCM1076+RTCM1077+RTCM1081+RTCM1082+RTCM1083+RTCM1084+RTCM1085+RTCM1086+RTCM1087+RTCM1091+RTCM1092+RTCM1093+RTCM1094+RTCM1095+RTCM1096+RTCM1097+RTCM1101+RTCM1102+RTCM1103+RTCM1104+RTCM1105+RTCM1106+RTCM1107+RTCM1111+RTCM1112+RTCM1113+RTCM1114+RTCM1115+RTCM1116+RTCM1117+RTCM1121+RTCM1122+RTCM1123+RTCM1124+RTCM1125+RTCM1126+RTCM1127+RTCM1230\r\n";
                byte[] bytecom3 = com3.getBytes();
                HardwareControler.write(devFD, bytecom3);

                Sleep();

                String com6 = "setPPSParameters, , , , , , 500.000\r\n";
                byte[] bytescom6 = com6.getBytes();
                HardwareControler.write(devFD, bytescom6);

                Sleep();

                String com4 = "setSignalTracking, GPSL1CA+GPSL1PY+GPSL2PY+GPSL2C+GPSL5+GLOL1CA+GLOL2P+GLOL2CA+GLOL3+GALL1BC+GALE5a+GALE5b+GALE5+GEOL1+GEOL5+BDSB1I+BDSB2I+BDSB3I+BDSB1C+BDSB2a+QZSL1CA+QZSL2C+QZSL5+NAVICL5\r\n";
                byte[] bytescom4 = com4.getBytes();
                HardwareControler.write(devFD, bytescom4);

                Sleep();

                String com5 = "setSignalUsage, GPSL1CA+GPSL1PY+GPSL2PY+GPSL2C+GLOL1CA+GLOL2P+GLOL2CA+GALL1BC+GALE5a+GALE5b+GALE5+GEOL1+GEOL5+BDSB1I+BDSB2I+BDSB3I+BDSB1C+BDSB2a+QZSL1CA+QZSL2C\r\n";
                byte[] bytescom5 = com5.getBytes();
                HardwareControler.write(devFD, bytescom5);

                Sleep();

                String com7 = "setSignalUsage, , GPSL1CA+GLOL1CA+GLOL2CA+GALL1BC+GALE5a+GALE5b+GALE5+GEOL1+BDSB1I+BDSB2I+BDSB3I+BDSB1C+BDSB2a+QZSL1CA\r\n";
                byte[] bytescom7 = com7.getBytes();
                HardwareControler.write(devFD, bytescom7);

                Sleep();

                String com8 = "exeCopyConfigFile,Current,Boot\r\n";
                byte[] bytescom8 = com8.getBytes();
                HardwareControler.write(devFD, bytescom8);

                Sleep();

//                //读取i2c文件夹
                fileContent = FileUtils.getFileContent(new File(path + "/storage/emulated/0/" + "iN6100_data/NMEA/" + "NMEA.txt"));
                SendI2cCmd(fileContent);

            }
        });

        consoleImage.setImageResource(R.drawable.action_selected);
        consoleText.setTextColor(getResources().getColor(R.color.app_main));
        aboutusLayout = (TextView) findViewById(R.id.about_relative);

        aboutusLayout.setOnClickListener(this);

        switch (FileManager.getInstance().get_FileStreamSta()) {
            case FileManager.ISRECORDING: {
                recordLayout.setClickable(false);
                playLayout.setClickable(false);
                deleteLayout.setClickable(false);
                pauseLayout.setClickable(true);
                recordLayout.setBackgroundColor(getResources().getColor(R.color.text_tertiary));
                playLayout.setBackgroundColor(getResources().getColor(R.color.text_tertiary));
                pauseLayout.setBackgroundColor(getResources().getColor(R.color.app_main));
            }
            break;
            case FileManager.ISPLAYING: {
                recordLayout.setClickable(false);
                playLayout.setClickable(false);
                deleteLayout.setClickable(false);
                pauseLayout.setClickable(true);
                recordLayout.setBackgroundColor(getResources().getColor(R.color.text_tertiary));
                playLayout.setBackgroundColor(getResources().getColor(R.color.text_tertiary));
                pauseLayout.setBackgroundColor(getResources().getColor(R.color.app_main));
            }
        }
    }

    private void validateUI() {
        switch (FileManager.getInstance().get_FileStreamSta()) {
            case FileManager.NORMAL: {
                if (FileManager.getInstance().getPlaybackFlag()) {
                    totalTimeTextView.setText("回放结束");
                    recordLayout.setBackgroundColor(getResources().getColor(R.color.app_main));
                    playLayout.setBackgroundColor(getResources().getColor(R.color.app_main));
                    pauseLayout.setBackgroundColor(getResources().getColor(R.color.text_tertiary));
                    recordLayout.setClickable(true);
                    playLayout.setClickable(true);
                    pauseLayout.setClickable(false);
                    deleteLayout.setClickable(true);
                }
            }
            break;
            case FileManager.ISPLAYING: {
                totalTimeTextView.setText("回放时间：" + FileManager.getInstance().getReplayTime() + "s/" + FileManager.getInstance().getWholeTime() + "s");
            }
            break;
            case FileManager.ISRECORDING: {
                totalTimeTextView.setText("录制时间：" + FileManager.getInstance().getRecordTime() + "s");
            }
        }
    }

    public void onEventMainThread(final EventSerialReceive event) {
        runOnUiThread(new Runnable() {
            public void run() {
                validateUI();
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (FileManager.getInstance().get_FileStreamSta()) {
            case FileManager.NORMAL: {
                switch (v.getId()) {
                    case R.id.about_relative: {
                        Intent aboutIntent = new Intent(this, AboutUsActivity.class);
                        startActivity(aboutIntent);
                    }
                    break;
                }
            }
            break;
            case FileManager.ISRECORDING: {
                switch (v.getId()) {
                    case R.id.about_relative: {
                        Intent aboutIntent = new Intent(this, AboutUsActivity.class);
                        startActivity(aboutIntent);
                    }
                    break;
                }
            }
            break;
            case FileManager.ISPLAYING: {
                switch (v.getId()) {
                    case R.id.about_relative: {
                        Intent aboutIntent = new Intent(this, AboutUsActivity.class);
                        startActivity(aboutIntent);
                    }
                    break;
                }
            }
        }
    }

    ListStyle.OnItemAcceptedListener chooseListener = new ListStyle.OnItemAcceptedListener<String>() {
        @Override
        public void onItemAccepted(String item, int position) {
            //click stop button
            if (currentAction == 0) { // replay
                totalTimeTextView.setText("回放时间：0s/" + FileManager.getInstance().getWholeTime() + "s");
                if (FileManager.getInstance().StartReplaying(item) == false) {
                    recordLayout.setClickable(true);
                    playLayout.setClickable(true);
                    pauseLayout.setClickable(false);
                    deleteLayout.setClickable(true);
                    recordLayout.setBackgroundColor(getResources().getColor(R.color.app_main));
                    playLayout.setBackgroundColor(getResources().getColor(R.color.app_main));
                    pauseLayout.setBackgroundColor(getResources().getColor(R.color.text_tertiary));
                    totalTimeTextView.setText("回放失败!");
                    FileManager.getInstance().StopReplaying();
                }
            } else {  //delete
                deleteLayout.setBackgroundColor(getResources().getColor(R.color.red_700));
                String fileName = item;
                FileManager.getInstance().DeleteFile(fileName);
            }
        }
    };

    private boolean SendI2cCmd(String s) {
        boolean ret = true;
        byte[] bytes = s.getBytes();
        byte b;
        for (int i = 0; i < bytes.length; i++) {
            b = bytes[i];
            if (HardwareControler.I2CWriteByte(dev, b, 20) != 0) {
                ret = false;
                break;
            }
        }
        return ret;
    }

    private void Sleep() {
        try {
            Thread.sleep(100);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
